package com.huangmb.idea.utils;

import com.huangmb.idea.bean.TranslateTask;

/**
 * Created by huangmb on 2016/12/11.
 */
public class TranslateUtils {

    private static final String NAME = "TranslateUtils";
    private static TranslateTask lastTask;

    public static void query(String query, TaskManager.CallBack callBack){

        TaskManager manager = TaskManager.getInstance();
        //if (lastTask != null){
            //manager.shutdown(lastTask);
        //}
        TranslateTask task = new TranslateTask(strip(addBlanks(query)));
        lastTask = task;
        manager.execute(task, callBack);
    }
    private static String addBlanks(String str) {
        String temp = str.replaceAll("_", " ");
        if (temp.equals(temp.toUpperCase())) {
            return temp;
        }
        return temp.replaceAll("([A-Z]+)", " $0");
    }

    private static String strip(String str) {
        return str.replaceAll("/\\*+", "")
                .replaceAll("\\*+/", "")
                .replaceAll("\\*", "")
                .replaceAll("//+", "")
                .replaceAll("\r\n", " ")
                .replaceAll("\\s+", " ");
    }
}
